package com.gonsin.i18n.lib;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;

import androidx.annotation.MainThread;
import androidx.core.view.LayoutInflaterCompat;

import com.gonsin.i18n.lib.common.TextObserver;
import com.gonsin.i18n.lib.common.TextResult;
import com.gonsin.i18n.lib.config.TConfig;
import com.gonsin.i18n.lib.utils.ThreadUtils;
import com.gonsin.i18n.lib.view.TextInflaterFactory2;
import com.gonsin.i18n.lib.view.TextManager;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class T {

    /**
     * 切換字符串工厂
     */
    public static void initFactory(Activity activity){
        LayoutInflaterCompat.setFactory2(activity.getLayoutInflater(), new TextInflaterFactory2(null));
    }

    /**
     * 切換字符串工厂(有其他皮肤框架时调用该方法)
     */
    public static void initFactory(Activity activity, LayoutInflater.Factory2 skinFactory){
        LayoutInflaterCompat.setFactory2(activity.getLayoutInflater(), new TextInflaterFactory2(skinFactory));
    }

    /**
     * 设置当前语言
     */
    public static void setLang(String lang){
        TextManager.getInstance().setLang(lang);
    }


    /**
     * @deprecated 后续不再在T方法内做format工作
     * 
     * 将本地字符串转化成翻译后的字符串
     */
    public static String t(Context context, int resId, Object ... args){
        String origin = context.getString(resId);
        if(origin == null){
            return "";
        }
        return TextManager.getInstance().getText(origin, args);
    }


    /**
     * 将本地字符串转化成翻译后的字符串
     */
    public static String t(Context context, int resId){
        String origin = context.getString(resId);
        if(origin == null){
            return "";
        }
        return TextManager.getInstance().getText(origin);
    }


    /**
     * 将本地字符串转化成翻译后的字符串
     * @param common 翻译人员看到的内容
     */
    public static String text(Context context, int resId, String common){
        String origin = context.getString(resId);
        if(origin == null){
            return "";
        }
        return TextManager.getInstance().getTextWithCommon(origin, common);
    }


    /**
     * 将本地字符串数组转化成翻译后的字符串数组
     */
    public static String[] array(Context context, int resId){
        return array(context, resId, null);
    }


    /**
     * 将本地字符串数组转化成翻译后的字符串数组
     * @param common 翻译人员看到的内容
     */
    public static String[] array(Context context, int resId, String common){
        String[] origin = context.getResources().getStringArray(resId);
        if(origin == null){
            return null;
        }
        if(origin.length == 0){
            return origin;
        }
        String[] after = new String[origin.length];
        for(int i = 0; i < origin.length; i++){
            after[i] = TextManager.getInstance().getTextWithCommon(origin[i], common);
        }
        return after;
    }


    /**
     * 初始化
     */
    @MainThread
    public static void config(TConfig config) {
        TextManager.getInstance().config(config);
    }

    /**
     * 异步加载字符串资源
     */
    public static Future<TextResult> load() {
        return ThreadUtils.submit(new Callable<TextResult>() {
            @Override
            public TextResult call() throws Exception {
                return loadSync();
            }
        });
    }

    /**
     * 添加字符串观察者
     * @param observer
     */
    public static void addObserver(TextObserver observer){
        TextManager.getInstance().addObserver(observer);
    }

    /**
     * 移除字符串观察者
     * @param observer
     */
    public static void removeObserver(TextObserver observer){
        TextManager.getInstance().removeObserver(observer);
    }


    /**
     * 同步加载字符串
     */
    public static TextResult loadSync(){
        return TextManager.getInstance().loadSync();
    }

    public static void upload() {
        TextManager.getInstance().uploadNewTexts();
    }


    /**
     * 同步加载字符串
     */
    public static boolean isLoadFinished(){
        return TextManager.getInstance().isLoadFinished();
    }
}
